
var boxname = null;
var setting = 0;
var basepath = '';
function UbbBox()
{
	return $(boxname);
}


function CreateUbbEditor(name, st, bp)
{
	boxname = name;
	if(!undefined(st))
		setting = st;
	if(!undefined(bp))
		basepath = bp;
		
	InitToolbar(false);
}

function GetUbbEditor(name, st, bp)
{
	boxname = name;
	if(!undefined(st))
		setting = st;
	if(!undefined(bp))
		basepath = bp;
		
	return InitToolbar(true);
}
function InitToolbar(flag)
{
	var buttons = ['bold', 'italic', 'underline',
				   'justifyleft', 'justifycenter', 'justifyright', 'createlink','image', 'flash', 'real'
				  ];
	var bar = '';
	
	bar += "<select onchange=\"SetFont('fontname',  this, '" + boxname + "')\"><option value=\"宋体\" selected=\"selected\">宋体</option><option value=\"黑体\">黑体</option><option value=\"sans-serif\">Sans-Serif</option><option value=\"arial\">Arial</option><option value=\"tahoma\">Tahoma</option><option value=\"verdana\">Verdana</option><option value=\"fixedsys\">FixedSys</option></select>";
	bar += " <select onchange=\"SetFont('forecolor', this, '" + boxname + "')\"><option value=\"black\" selected=\"selected\" style=\"background:black;color:black\">Black</option><option value=\"red\" style=\"background:red;color:red\">Red</option><option value=\"pink\" style=\"background:pink;color:pink\">Pink</option><option value=\"green\" style=\"background:green;color:green\">Green</option><option value=\"orange\" style=\"background:orange;color:orange\">Orange</option><option value=\"purple\" style=\"background:purple;color:purple\">Purple</option><option value=\"blue\" style=\"background:blue;color:blue\">Blue</option><option value=\"brown\" style=\"background:brown;color:brown\">Brown</option><option value=\"teal\" style=\"background:teal;color:teal\">Teal</option><option value=\"navy\" style=\"background:navy;color:navy\">Navy</option><option value=\"maroon\" style=\"background:maroon;color:maroon\">Maroon</option><option value=\"limegreen\" style=\"background:limegreen;color:limegreen\">LimeGreen</option></select>";
	bar += " <select onchange=\"SetFont('fontsize',  this, '" + boxname + "')\"><option value=\"1\" selected=\"selected\">1</option><option value=\"2\">2</option><option value=\"4\">4</option><option value=\"6\">6</option><option value=\"8\">8</option></select>";
	
	for (var i = 0; i < buttons.length; i++) {
		
		if (buttons[i] == 'image')
			if ((setting & 2) == 0) continue;
		if (buttons[i] == 'flash')
			if ((setting & 4) == 0) continue;
		if (buttons[i] == 'real')
			if ((setting & 8) == 0) continue;
		
		bar += ' ' + '<a href="javascript:UbbCmd(\'' + buttons[i] + '\', false, \'\', \'' + boxname + '\');"><img src="' + basepath + 'images/ubb/' + buttons[i] + '.gif" alt="" /></a>';
	}
	if (flag) return bar;
	else
	document.write(bar);
}

function UbbCmd(sCmd, bInterface, vValue, c) {
	boxname = c;
	UbbBox().focus();
	
	if (sCmd == 'createlink')
	{
		var r = prompt("请输入链接", "");
		if (!undefined(r) && r != null)
		{
			InsertUbb('[url=' + r + ']' + r + '[/url]');
		}
		return;
	}
	
	 
	if (sCmd == 'image')
	{
		var r = prompt("请输图片 URL 地址", "");
		if (!undefined(r) && r != null)
		{
		 	InsertUbb('[img]' + r + '[/img]');
		}
		return;
	}
	
	if (sCmd == 'flash')
	{
		var r = prompt("请输入 Flas h地址", "");
		if (!undefined(r) && r != null && r != '')
		{
			InsertUbb('[flash=600,400]' + r + '[/flash]');
		}
		return;
	}
	
	if (sCmd == 'real')
	{
		var r = prompt("请输入视频地址", "");
		if (!undefined(r) && r != null && r != '')
		{
		 	InsertUbb('[rm=600,400]' + r + '[/rm]');
		}
		return;
	}
	
	var index = 0;
		
	var arr = {
		bold : [[ '[b]', '[/b]' ],[ '<b>', '</b>' ]],
		italic : [[ '[i]', '[/i]' ],[ '<i>', '</i>' ]],
		underline : [[ '[u]', '[/u]' ],[ '<u>', '</u>' ]],
		justifyleft : [[ '[align=left]', '[/align]' ],[ '<p align="left">', '</p>' ]],
		justifycenter : [[ '[align=center]', '[/align]' ],[ '<p align="center">', '</p>' ]],
		justifyright : [[ '[align=right]', '[/align]' ],[ '<p align="right">', '</p>' ]],
		insertorderedlist : [[ '[list=1][*]', '[/list]'],[ '<ol><li>', '</li></ol>']],
		insertunorderedlist : [[ '[list][*]', '[/list]' ],[ '<ul><li>', '</li></ul>' ]],
		indent : [[ '[indent]', '[/indent]' ], [ '<blockquote>', '</blockquote>' ]],
		subscript : [[ '[sub]', '[/sub]' ],[ '<sub>', '</sub>' ]],
		superscript : [[ '[sup]', '[/sup]' ],[ '<sup>', '</sup>' ]],
		fontname : [['[font=' + vValue + ']', '[/font]' ],['<font face="' + vValue + '">', '</font>' ]],
		forecolor : [['[color=' + vValue + ']', '[/color]' ],['<font color="' + vValue + '">', '</font>' ]],
		fontsize : [['[size=' + vValue + ']', '[/size]' ],['<font size="' + vValue + '">', '</font>' ]]
	};
	
	for (var a in arr) {
		if (a == sCmd) {
			ApplyUbb(arr[a][index][0], arr[a][index][1]);
			break;
		}
	}
}

function SetFont(item, obj, c) {
	UbbCmd(item, false, obj.options[obj.selectedIndex].value, c);
}

function InsertUbb(text) {
	UbbBox().focus();
	if (document.selection && document.selection.createRange) {
		var range = document.selection.createRange();
		range.text = text;
	} else {
		UbbBox().value += text;
	}
}

function ApplyUbb(prepend, append) {
	UbbBox().focus();
	if (document.selection && document.selection.createRange) {
		var range = document.selection.createRange();
		range.text = prepend + range.text + append;					
	} else {
		UbbBox().value += text;
	}
}

function Insert(str) {
	InsertUbb(str);
}